package graph;

import java.util.Comparator;

/**
 *
 * @author Roberto Maestre Martínez <roberto.maestre AT cchs.csic.es>
 */
public abstract class Graph implements GraphBehaviur {

   //Inner class to order Integer vectors
   private class Orderasc  implements Comparator<Integer> {
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }


   /**
    * * @param N The Matrix size (N*N)
    */
   protected int N;


   /**
    * * @param m The Matrix structure
    */
   protected int [][]m;


   /**
    * * @param  Initial value
    */
   protected int INF;


   protected boolean[] visits;
   protected int auxInt;
   

   public int[][] getM() {
        return m;
    }


   /**
    * Create an array with N columns and N rows.
    * Warning: the index vector are: 0,1,2,3,....,N-1
    *
    * @param  N The Matrix size (N*N)
    */
   public void createMatrix(int N) throws Exception{
        this.N=N+1;
        initMatrix();
    }


   /**
    * Initialize the matriz with 0 value in nodes
    */
   private void initMatrix(){
      INF=Integer.MAX_VALUE;
      m = new int[N][N];
      for (int a=1;a<N;a++){
        for (int b=1;b<N;b++) {
            m[a][b]=INF;
      }
      }
    }



   /**
    * Gets the weight of specific edge
    *
    * @param  The weight of edge
    */
   public int getEdgeWeight(int x, int y){
       return m[x][y];
   }

   
    @Override
   public String toString(){
       String string="Index:( ";
       //Create index
       for (int a=0;a<N;a++) string+=Integer.toString(a);
       //Create matrix
       string+=") _ Matrix:";
       for (int a=0;a<N;a++) {
        string+="[ ";
        for (int b=0;b<N;b++) string+=Integer.toString(m[a][b])+" ";
        string+="]";
       }
       return string;
   }


   /**
    * Cool print for numbers
    *
    * @return  String number with spaces
    */
   private String systemOutGetInt(int i){
       if (i==Integer.MAX_VALUE) return"I   ";
       else {
           if (i>9)
            return Integer.toString(i)+"  ";
           else
            return Integer.toString(i)+"   ";
       }
   }


   /**
    * Provides an interface for print all matrix N*N (cm,costs,m,ect...)
    *
    * @param  String number with spaces
    */
   protected void systemOutPrintGeneralMatrix(int[][] generalmatrix) throws Exception{
        System.out.print("     ");
        systemOutPrintIndex();
        System.out.println();
        for (int a=1;a<N;a++) {
            for (int b=1;b<N;b++) {
                if (b==1)
                    System.out.print(systemOutGetInt(a)+" "+systemOutGetInt(generalmatrix[a][b]));
                else System.out.print(systemOutGetInt(generalmatrix[a][b]));
                }
        System.out.println();
        }
    }

   /**
    * Print cool index with spaces
    *
    * @param  String index formated
    */
   public void systemOutPrintIndex(){
         for (int a=1;a<N;a++) {
            System.out.print(a+"  ");
            if (a<9) System.out.print(" ");
        }
    }


   /**
    * Provide command line output of graph matrix
    *
    */
   public void systemOutPrintMatrix() throws Exception{
        systemOutPrintGeneralMatrix(m);
    }


   /**
    * Get Size,Maxsize and Free size of JVM
    *
    * @return  String with all information
    */
   public String getHeapData(){
       return "Size: "+Runtime.getRuntime().totalMemory()+" Max:"+Runtime.getRuntime().maxMemory()+" Free:"+Runtime.getRuntime().freeMemory();
   }


   /**
    * Get Heap Size-Free heap size of JVM
    *
    * @return  long with size in bytes
    */
   public long getHeapUsed(){
       return Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
   }

}
